#include "header.h"

/* Usage: ./05_dirent dir_path */
int main(int argc, char *argv[]){
    ARGS_CHECK(argc, 2);
    // 操作目录流的规范流程
    DIR *dirp = opendir(argv[1]);
    ERROR_CHECK(dirp, NULL, "opendir");

    // 中间进行读目录流的操作
    struct dirent *pdirent;
    long location;
    while((pdirent = readdir(dirp)) != NULL){
        printf("inode num = %ld, reclen = %d, type = %d, name = %s\n",
                pdirent->d_ino,
                pdirent->d_reclen,
                pdirent->d_type,
                pdirent->d_name);
         if(strcmp("a.out", pdirent->d_name) == 0){
             // 说明正在处理目录项file1
             // 但是telldir函数记录的目录流指针是目录项dir2
            location = telldir(dirp);
            printf("\n%ld\n", telldir(dirp));
         }
        
    }
    printf("----------------------------------\n");
    seekdir(dirp, location);
    // rewinddir(dirp);
    pdirent = readdir(dirp);
    printf("inode num = %ld, reclen = %d, type = %d, name = %s\n",
       pdirent->d_ino,
       pdirent->d_reclen,
       pdirent->d_type,
       pdirent->d_name);

    
    closedir(dirp);
    return 0;
}

